import jwt
import time
from config import Config
from loguru import logger


class JWTManager:
    secret_key = Config.SECRET_KEY  # 密钥

    @classmethod
    def generate(self,data:dict):
        """"
        生成加密后的token
        """
        try:
            data.update(timestamp = int(time.time()*1000))
            token = jwt.encode(data,self.secret_key,algorithm="HS256")
            return token
        except Exception as e:
            logger.error(f"异常原因:{e}")
            raise e

    @classmethod
    def decrypt(self,token:str):
        """
        对token进行解密
        """
        data = {}
        try:
            data = jwt.decode(token,self.secret_key,algorithms=["HS256"])
        except Exception as e:
            logger.error(f"异常原因:{e}")
        finally:
            return data

